package com.itboye.sunsunhome.www.aq;

import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.itboye.base.BaseActivity;
import com.itboye.sunsunhome.bean.AdBean;
import com.itboye.sunsunhome.util.GetModelInfo;
import com.itboye.sunsunhome.util.HttpRequest;
import com.itboye.sunsunhome.util.XImageLoader;
import com.itboye.sunsunhome.volley.MyJsonRequest;
import com.itboye.sunsunhome.volley.XErrorListener;
import com.itboye.sunsunhome.volley.XRequestListener;
import com.itboye.sunsunhome.www.aq.base.AqDevice;
import com.itboye.sunsunhome.www.aq.base.AqDeviceMessage;
import com.itboye.sunsunhome.www.aq.base.AqDeviceMessage.AqDeviceMessageType;
import com.itboye.sunsunhome.www.aq.base.MessageWhat;
import com.itboye.sunsunhome.www.aq.base.MyApplication;

public class FeedbackActivity extends BaseActivity implements TextWatcher{
	Button btnSend;
	EditText ediContext,edittmodel;
	ImageView fankuiback;
	CheckBox CheckBox;
	AqDeviceMessageType messageType;
	int progress=0;
	 private final int SUCCESS=0;
	@Override
	protected void onCreate(Bundle arg0) {
		// TODO Auto-generated method stub com.itboye.sunsunhome.www.aq.FeedbackActivity
		super.onCreate(arg0);
		setContentView(R.layout.activity_feedback);
		btnSend=(Button) findViewById(R.id.btnSend);
		ediContext=(EditText) findViewById(R.id.ediContext);
		edittmodel=(EditText) findViewById(R.id.edittmodel);
		fankuiback=(ImageView) findViewById(R.id.fankuiback);
		CheckBox=(android.widget.CheckBox) findViewById(R.id.checkbox);
		messageType= AqDeviceMessageType.Null;
		fankuiback.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});
		btnSend.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				
		    		getAdImage();
			}
		});
			
	}
	
	/** 
     * 展示一个特定颜色的Toast 
     * 
     * @param message 
     */  
    protected void OnToast(String message,Context context) {  
        View toastRoot = LayoutInflater.from(context).inflate(R.layout.toast, null);  
        Toast toast = new Toast(context);  
        toast.setView(toastRoot);  
        TextView tv = (TextView) toastRoot.findViewById(R.id.toast_notice);  
        tv.setText(message);  
        toast.setGravity(Gravity.CENTER, 0, 0);
        toast.show();  
        finish();
    }
    private void getAdImage() {
		// TODO Auto-generated method stub
    	
		MyJsonRequest.Builder<String> builder = new MyJsonRequest.Builder<String>()
				.apiVer("100").typeKey("BY_Suggest_add")
				.param("uid", "0")
				.param("text", ediContext.getText().toString()+"")
				.param("mobile", edittmodel.getText().toString()+"")
				.requestListener(new XRequestListener<String>() {

					@Override
					public void onResponse(String orderBean) {
						// onPullUpFinish(adapter, xlistview, orderBean);
						OnToast(""+orderBean, MyApplication.context);
						ediContext.setText("");
						edittmodel.setText("");
						
						
					}
				}).errorListener(new XErrorListener() {

					@Override
					public void onErrorResponse(Exception exception, int code,
							String msg) {
						Toast.makeText(MyApplication.context,"反馈失败！", 0).show();
					}
				});

		MyJsonRequest<String> request = builder.build();
		HttpRequest.getDefaultRequestQueue().add(request);
	}
    
    public void  onSengMessge() throws JSONException{
    	MyJsonRequest<String> request = new MyJsonRequest.Builder<String>()
				.apiVer("100")
				.typeKey("BY_Log_deviceLog")//错误日志上传
				.param("context", onJSONObject())//上传手机型号、手机ip、手机所在网络
				.param("log", messageType)
				.param("type", "DEVICE_LINKING")
				.requestListener(
						new XRequestListener<String>() {

							@Override
							public void onResponse(String arg0) {
								Log.v("aq", "AqDevice-------");
							}
						}).errorListener(new XErrorListener() {

					@Override
					public void onErrorResponse(Exception exception, int code,
							String msg) {
						
					}
				}).build();
		HttpRequest.getDefaultRequestQueue().add(request);
		
    }
    
   
    public  Object   onJSONObject() throws JSONException{
    	JSONObject out;
    	out = new JSONObject();

		JSONObject info = new JSONObject();
		try {
			out.put("did", GetModelInfo.GetNetworkType());
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		out.put("type", "DEVICE_LINKING");
		out.put("context", info);
		out.put("log", "一键配置超时");
		return out;
    }
	@Override
	public void beforeTextChanged(CharSequence s, int start, int count,
			int after) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void onTextChanged(CharSequence s, int start, int before, int count) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void afterTextChanged(Editable s) {
		// TODO Auto-generated method stub
		if(ediContext.getText().length()<=0){
			Toast.makeText(MyApplication.context, "请输入反馈内容！", 3).show();
			btnSend.setEnabled(false);
		}else{
			btnSend.setEnabled(true);
		}
	}

}
